﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;


namespace PurpleKnife.Library
{
    public class Configuration
    {
        private string _configFilePath;
        private pkGroup _rootGroup;

        public pkGroup RootGroup
        {
            set { _rootGroup = value; }
            get { return _rootGroup; }
        }
        
        public string ConfigFilePath
        {
            set { _configFilePath = value; }
            get { return _configFilePath; }
        }

        public void Save()
        {
            DoSave();
        }

        public void Load()
        {
            DoLoad();
        }

        public void Load(string FilePath)
        {
            _configFilePath = FilePath;
            DoLoad();
        }

        public void Save(string FilePath)
        {
            _configFilePath = FilePath;
            DoSave();
        }

        private void DoSave()
        {
            FileStream fs = new FileStream(_configFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, _rootGroup);
            fs.Close();
        }

        private void DoLoad()
        {
            FileStream fs = new FileStream(_configFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryFormatter bf = new BinaryFormatter();
            _rootGroup = (pkGroup)bf.Deserialize(fs); 
            fs.Close();
        }
    }
}
